<?php

namespace App\Http\Controllers\Business;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

/**
 * 订单控制器类
 * Class OrderController
 * @package App\Http\Controllers\business
 */
class OrderController extends Controller
{
    /**
     * 显示订单主页
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request) {

        $num = $request->input('num', 10);

        $data = DB::table('orders')
            ->where(function ($query) use($request){
                $query->where('ordercode', 'like', '%'.$request->input('keyword').'%')->where('created_at', '!=', '');
            })
            ->paginate($num);

        return view(('business.order.index'), ['data' => $data, 'request' => $request->all()]);
    }

    /**
     * 显示订单添加页面
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add() {
        return view('business.order.add');
    }

    /**
     * 执行订单ajax更新操作
     * @param Request $request
     */
    public function ajaxUpdate(Request $request) {

        $id = $request->input('id');
        $orderstatus = $request->input('orderstatus');

        $data = DB::table('orders')->where('id', '=', $id)->update(['status' => ($orderstatus + 1), 'updated_at' => date("Y-m-d H:i:s", time())]);

        echo $data;
//        return response()->json(34);
    }

    /**
     * 执行订单删除操作
     * @param $id
     * @return \Illuminate\Http\RedirectResponse
     */
    public function delete($id) {

        $updated_at = date("Y-m-d H:i:s", time());
        $res = DB::table('orders')->where('id', '=', $id)->update(['created_at' => '', 'updated_at' => $updated_at]);

        if ($res) {
            return redirect('business/order/index')->with(['info' => '删除成功']);
        } else {
            return redirect('business/order/index')->with(['info' => '删除失败']);
        }

    }
}
